﻿using ModernChannel.Model.Interface;
using ModernChannel.Model.Model;
using ModernChannel.Model.Model.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModernChannel.Provider
{
    public class OrganizationProvider : GenericProvider, IOrganization
    {
        public List<Organization> SearchOrganizations(out int totalRecord, string textSearch, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<Organization>();
            if (!string.IsNullOrWhiteSpace(textSearch))
                querry = querry.Where(x => x.OrgName.ToUpper().Contains(textSearch.ToUpper()) || x.OrgCode.ToUpper() == textSearch.ToUpper());

            querry = querry.OrderBy(x => x.OrgCode);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public List<Organization> SearchOrganizations(out int totalRecord, string organizationCode, string organizationName, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<Organization>();
            if (!string.IsNullOrWhiteSpace(organizationCode))
                querry = querry.Where(x => x.OrgCode.ToUpper() == organizationCode.ToUpper());

            if (!string.IsNullOrWhiteSpace(organizationName))
                querry = querry.Where(x => x.OrgName.ToUpper().Contains(organizationName.ToUpper()));

            querry = querry.OrderBy(x => x.OrgCode);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public Organization RetrieveOrganization(string organizationCode)
        {
            var code = organizationCode ?? string.Empty;
            return repository.FindOne<Organization>(x => x.OrgCode.ToUpper() == code);
        }

        public bool Add(Organization record)
        {
            try
            {
                if (RetrieveOrganization(record.OrgCode) == null)
                {
                    repository.Add<Organization>(record);
                    repository.UnitOfWork.SaveChanges();
                }
                else
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(Organization record)
        {
            try
            {
                var updateRecord = RetrieveOrganization(record.OrgCode);
                if (updateRecord == null) return false;
                updateRecord.OrgName = record.OrgName;
                updateRecord.Description = record.Description;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string organizationCode)
        {
            try
            {
                repository.Delete<Organization>(x => x.OrgCode.ToUpper() == organizationCode);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
